Pick analysis point
Description
-
This is a custom interactive tool for selecting the civil points and generating report data for those points.
-
The user is prompted for civil points as Occupied point, Foresight point and Backsight point which user needs to select from UI.
-
The PickAnalysisPoints class which extends DgnElementSetTool which handles different events to interact with UI, the PickAnalysisPoints class overrides the events here in this tool.
Note: Use OpenRoads Designer and SDK version 10.12 or above
Remarks
- This sample code is a part of ManagedSDKExample which you get with SDK installation under "examples" section in SDK installation directory.
- If you encounter any error while using DgnElementSetTool class, make sure to add a reference to Bentley.DgnDisplayNet.dll by selecting Project > Add Reference or change the projects .csproj file to add reference to this dll .
- The default dll location will be "C:\Program Files\Bentley\OpenRoads Designer CE 10.11\OpenRoadsDesigner\Bentley.DgnDisplayNet.dll"
- The method OnDataButton() handles the point selection from User interface.
Source Code
using System;
using System.Collections.Generic;
using Bentley.CifNET.SDK.Edit;
using Bentley.GeometryNET;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.CifNET.Formatting;
namespace ExampleSnippetAddIn.Examples
{
class PickAnalysisPoints : DgnElementSetTool
{
public enum State
{
OccupiedPoint = 0,
BackSightPoint,
ForeSightPoint,
}
State m_state = State.OccupiedPoint;
PointEntity2dInPlan m_backSightPoint, m_occupiedPoint;
List<PointEntity2dInPlan> m_foreSightPoints = new List<PointEntity2dInPlan>();
internal static DgnModel m_activeModel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
public PickAnalysisPoints() : base()
{
}
protected override void OnRestartTool()
{
InstallNewInstance();
}
protected override void ExitTool()
{
base.ExitTool();
}
protected override void OnPostInstall()
{
base.BeginPickElements();
Bentley.DgnPlatformNET.AccuSnap.LocateEnabled = true;
Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
ConsensusConnection con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
if (con == null)
return;
base.OnPostInstall();
m_state = State.OccupiedPoint;
NotificationManager.OutputPrompt("Select Occupied Point.");
}
protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
{
Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 0);
if (hitPath == null)
return false;
Element el = hitPath.GetCursorElement();
if (el == null)
return false;
ConsensusConnection con = ConsensusConnectionEdit.GetActive();
PointEntity2dInPlan pointEntity2d = (el.ParentElement == null) ? PointEntity2dInPlan.CreateFromElement(con, el) : PointEntity2dInPlan.CreateFromElement(con, el.ParentElement);
if (pointEntity2d == null)
return false;
switch (m_state)
{
case State.OccupiedPoint:
m_occupiedPoint = pointEntity2d;
m_state = State.BackSightPoint;
NotificationManager.OutputPrompt("Select Backsight Point.");
break;
case State.BackSightPoint:
m_backSightPoint = pointEntity2d;
m_state = State.ForeSightPoint;
NotificationManager.OutputPrompt("Select Foresight Points.");
break;
case State.ForeSightPoint:
m_foreSightPoints.Add(pointEntity2d);
NotificationManager.OutputPrompt("Select Foresight Points / Reset for report.");
break;
default:
break;
}
return true;
}
protected override bool OnPostLocate(HitPath path, out string cantAcceptReason)
{
//checks that hitpath is not null
if (path == null)
{
cantAcceptReason = "HitPath is null.";
return false;
}
//checks that the cursor element is not null
Element element = path.GetCursorElement();
if (element == null)
{
cantAcceptReason = "There is no element at cursor.";
return false;
}
Bentley.CifNET.SDK.ConsensusConnection con = ConsensusConnectionEdit.GetActive();
if (con == null)
{
cantAcceptReason = "There was an error connecting to the Civil SDK model.";
return false;
}
PointEntity2dInPlan pointEntity2d = (element.ParentElement == null) ? PointEntity2dInPlan.CreateFromElement(con, element) : PointEntity2dInPlan.CreateFromElement(con, element.ParentElement);
if (pointEntity2d == null)
{
cantAcceptReason = "This is not a Civil 2d point.";
return false;
}
if (pointEntity2d == m_occupiedPoint)
{
cantAcceptReason = "Element has already been selected as the Occupied Point.";
return false;
}
else if (pointEntity2d == m_backSightPoint)
{
cantAcceptReason = "Element has already been selected as the Backsight Point.";
return false;
}
else
{
foreach (PointEntity2dInPlan elem in m_foreSightPoints)
{
if (pointEntity2d == elem)
{
cantAcceptReason = "Element has already been selected as a Foresight Point.";
return false;
}
}
}
cantAcceptReason = System.String.Empty;
return true;
}
public override Bentley.DgnPlatformNET.StatusInt OnElementModify(Bentley.DgnPlatformNET.Elements.Element element)
{
return Bentley.DgnPlatformNET.StatusInt.Error;
}
protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
{
if (m_state == State.ForeSightPoint)
{
m_state = State.OccupiedPoint;
MakeReport();
//Clean-up
NotificationManager.OutputPrompt("Select Occupied Point.");
m_foreSightPoints.Clear();
m_occupiedPoint = null;
m_backSightPoint = null;
}
else
{
ExitTool();
}
return true;
}
public static void InstallNewInstance()
{
PickAnalysisPoints tool = new PickAnalysisPoints();
tool.InstallTool();
}
//Code from PointAnalysisReporter
public struct Data
{
public DPoint3d Object;
public string Name;
public double Bearing;
public double Angle;
public double Distance;
public double Northing;
public double Easting;
}
private void DataMover(PointEntity2dInPlan obj, State pt_lvl)
{
Bentley.DgnPlatformNET.ModelInfo info = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel().GetModelInfo();
Data data = new Data();
DPoint3d coordinateObj = obj.Coordinates;
DPoint3d coordinateOP = m_occupiedPoint.Coordinates;
DPoint3d coordinateOP2 = coordinateOP;
coordinateOP2.X += 25; //An arbitrary number, intended to create a data point from which to construct a vector.
DVector3d opLine = new DVector3d(coordinateOP, coordinateOP2);
DVector3d occupiedToObject = new DVector3d(coordinateOP, coordinateObj);
data.Object = coordinateObj;
data.Name = obj.Name;
data.Northing = obj.Coordinates.Y / info.UorPerMeter;
data.Easting = obj.Coordinates.X / info.UorPerMeter;
data.Distance = coordinateObj.Distance(coordinateOP) / info.UorPerMeter;
if (pt_lvl == State.ForeSightPoint)
{
DPoint3d coordinateBSP = m_backSightPoint.Coordinates;
DVector3d occupiedToBacksight = new DVector3d(coordinateOP, coordinateBSP);
if (occupiedToBacksight.AngleToXY(occupiedToObject).Radians <= 0)
{
data.Angle = -1.0 * occupiedToBacksight.AngleToXY(occupiedToObject).Radians;
}
else
{
data.Angle = 2.0 * Math.PI - occupiedToBacksight.AngleToXY(occupiedToObject).Radians;
}
}
data.Bearing = occupiedToObject.AngleXY.Radians;
ReadPoint(data, pt_lvl);
}
private void MakeReport()
{
DataMover(m_backSightPoint, State.BackSightPoint);
DataMover(m_occupiedPoint, State.OccupiedPoint);
for (int i = 0; i < m_foreSightPoints.Count; i++)
{
DataMover(m_foreSightPoints[i], State.ForeSightPoint);
}
}
internal Dictionary<string, string> ReadPoint(Data point, State pt_lvl)
{
Dictionary<string, string> pointProperties = new Dictionary<string, string>();
if (point.Name == null || point.Name == "")
{
point.Name = "Unnamed";
}
pointProperties.Add("Name", point.Name);
if (pt_lvl != State.OccupiedPoint)
{
pointProperties.Add("Bearing", FormatDirection(point.Bearing));
pointProperties.Add("Distance", FormatDistance(point.Distance));
}
if (pt_lvl == State.ForeSightPoint)
{
pointProperties.Add("Angle Right", FormatAngle(point.Angle));
pointProperties.Add("Angle Left", FormatAngle(2 * Math.PI - point.Angle));
pointProperties.Add("Deflection Angle", FormatAngle(point.Angle - Math.PI));
}
pointProperties.Add("Northing", FormatForDisplay.Coordinate(point.Northing, m_activeModel));
pointProperties.Add("Easting", FormatForDisplay.Coordinate(point.Easting, m_activeModel));
return pointProperties;
}
private static string FormatDirection(double value)
{
ModelInfo info = m_activeModel.GetModelInfo();
return FormatForDisplay.Direction(value, m_activeModel);
}
private static string FormatDistance(double value)
{
return FormatForDisplay.Distance(value, m_activeModel, 3);
}
private static string FormatAngle(double valueRadians)
{
ModelInfo info = m_activeModel.GetModelInfo();
return FormatForDisplay.Angle(valueRadians, m_activeModel);
}
}
}